package com.zixun.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.zixun.Models.Message;
import com.zixun.Models.UserInfo;
import com.zixun.R;
import com.zixun.db.DatabaseUtil;
import com.zixun.main.MainActivity;

import java.util.List;


/**
 * 作者：郭郭 on 2017-3-21 10:01
 *  注册
 */

public class RegisterActivity extends Activity {
    private EditText mPhoneView;
    private EditText mPasswordView;
    private View mLoginFormView;
    private Button btn_register;
    private CheckBox chb_eye;
    private Context context;
    private DatabaseUtil mDBUtil;
    private List<Message> list = null;
    private List<Message> list1 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        context = this;
        mPhoneView = (EditText) findViewById(R.id.et_phone);
        mPasswordView = (EditText) findViewById(R.id.et_password);
        btn_register = (Button) findViewById(R.id.btn_register);
        btn_register.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptRegister();
            }
        });

        chb_eye = (CheckBox) findViewById(R.id.chb_eye);
        chb_eye.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int selection = mPasswordView.getSelectionStart();
                if (chb_eye.isChecked()) {
                    mPasswordView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPasswordView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                mPasswordView.setSelection(selection);
            }
        });

        mLoginFormView = findViewById(R.id.login_form);
        mDBUtil = new DatabaseUtil(RegisterActivity.this);

    }


    /**
     * 用户名密码输入校验
     */
    private void attemptRegister() {
        final String phone = mPhoneView.getText().toString();
        final String password = mPasswordView.getText().toString();
        if (TextUtils.isEmpty(phone) || phone.length()!=11) {
            Toast.makeText(context, "手机号输入有误", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(context, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        mDBUtil = new DatabaseUtil(RegisterActivity.this);
        mDBUtil.InsertUser(userInfo);
        startMainActivity();
    }





    private void startMainActivity() {
        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        finish();
    }


    @Override
    public void onBackPressed() {
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
        System.exit(0);
    }
}

